
package com.tools.cleanmaster.model.item;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.bean.ApkFileInfo;
import com.tools.cleanmaster.utils.PackageUtils;

import java.io.File;
import java.lang.ref.SoftReference;

/**
 * APK安装包类型的垃圾项（{@link TrashType#APK_FILE}）
 */
public class ApkFileItem extends TrashItem {
    /** 破损APK文件 */
    public static final int FLAG_BROKEN = 0x1;

    /** 旧版本APK文件 */
    public static final int FLAG_OLD = 0x2;

    /** 已安装版本APK文件 */
    public static final int FLAG_INSTALLED = 0x4;

    /** 新版本APK文件 */
    public static final int FLAG_NEW = 0x8;

    /** 重复APK文件 */
    public static final int FLAG_DUPLICATE = 0x10;

    /** 重复APK文件中的第一个 */
    public static final int FLAG_DUPLICATE_FIRST = 0x20;

    public int flag;

    public int versionCode;

    public long apkCreateTime;

    public String versionName;

    /** 应用图标 */
    private SoftReference<Drawable> appIcon;

    public ApkFileItem() {
        trashType = TrashType.APK_FILE;
    }

    /**
     * 判断该APK是否包含指定的标识
     * 
     * @param flagToCheck {@link #FLAG_BROKEN}, {@link #FLAG_OLD},
     *            {@link #FLAG_INSTALLED}, {@link #FLAG_NEW},
     *            {@link #FLAG_DUPLICATE}, {@link #FLAG_DUPLICATE_FIRST}
     */
    public boolean hasFlag(int flagToCheck) {
        return (flag & flagToCheck) == flagToCheck;
    }

    /**
     * 判断该应用是否已经安装
     */
    public boolean isAppInstalled() {
        return (flag & (FLAG_OLD | FLAG_INSTALLED | FLAG_NEW)) > 0;
    }

    /**
     * 刷新应用安装包的安装的状态，重置安装标识{@link #FLAG_OLD}, {@link #FLAG_INSTALLED},
     * {@link #FLAG_NEW}，此包名的应用安装包安装或卸载后调用此方法
     */
    public void refreshInstallStatus(Context cxt) {
        // reset install flags
        flag = flag & ~(FLAG_OLD | FLAG_INSTALLED | FLAG_NEW);
        try {
            PackageInfo pkgInfo = cxt.getPackageManager().getPackageInfo(pkgName, 0);
            if (pkgInfo != null) {
                if (versionCode < pkgInfo.versionCode) {
                    flag = flag | ApkFileItem.FLAG_OLD;
                } else if (versionCode == pkgInfo.versionCode) {
                    flag = flag | ApkFileItem.FLAG_INSTALLED;
                } else {
                    flag = flag | ApkFileItem.FLAG_NEW;
                }
            }
        } catch (NameNotFoundException e) {
            // package uninstalled
        }
    }
    /**
     * 需要在初始化filePath信息后才调用此函数
     * @param apkPkgInfo
     */
    public void initExtentAPKInfo(ApkFileInfo apkPkgInfo) {
        if (apkPkgInfo == null) {
            flag = ApkFileItem.FLAG_BROKEN;
            return;
        }
        versionCode = apkPkgInfo.versionCode;
        versionName = apkPkgInfo.versionName;
        PackageInfo pkgInfo = PackageUtils.getPkgInfo(CleanApplication.getInstance(),
                apkPkgInfo.pkgName);
        if (pkgInfo != null) {
            flag = ApkFileItem.FLAG_INSTALLED;
            isSelectedDefault = isSelected = true;
        }

        File apkFile = new File(filePath);
        apkCreateTime = apkFile.lastModified();
    }
}
